package client;

import entities.ImageMemory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author casshern
 */
public class BuyAndHire extends JPanel implements ActionListener {

    private JButton miner;
    private JButton laser;
    private JButton attack;
    private JButton cancelBuild;
    private Client client;
    private WorldPanel worldPanel;
    private BottomPanel bottomPanel;

    public BuyAndHire(Client client) {
        this.client = client;
        this.setPreferredSize(new Dimension(500, 110));
        this.setBackground(Color.ORANGE);
        this.setBorder(BorderFactory.createTitledBorder("Buy or Hire"));

        //==========================================================================
        // Button Part
        //==========================================================================
        // miner = new JButton("Miner");
        miner = new JButton("Miner", ImageMemory.Miner);
        miner.setPreferredSize(new Dimension(140, 60));
        miner.setForeground(Color.red);
        miner.addActionListener(this);
        laser = new JButton("Laser", ImageMemory.LASER);
        laser.setPreferredSize(new Dimension(140, 60));
        laser.setForeground(Color.red);
        laser.addActionListener(this);
        attack = new JButton("Attack", ImageMemory.ATTACK);
        attack.setPreferredSize(new Dimension(140, 60));
        attack.setForeground(Color.red);
        attack.addActionListener(this);
        cancelBuild = new JButton("X");
        cancelBuild.setPreferredSize(new Dimension(50, 60));
        cancelBuild.setEnabled(false);
        cancelBuild.addActionListener(this);

        this.add(miner);
        this.add(laser);
        this.add(attack);
        this.add(cancelBuild);
        JLabel cost = new JLabel();
        cost.setPreferredSize(new Dimension(500, 20));
        cost.setText("                     $ 100                                     $ 100                                     $ 100");
        this.add(cost);
    }

    public void setWorldPanel(WorldPanel worldPanel) {
        this.worldPanel = worldPanel;
    }

    public void setBottomPanel(BottomPanel bottomPanel) {
        this.bottomPanel = bottomPanel;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == laser) {
            buttonPress();
            try {
                client.send("laser");
            } catch (IOException ex) {
                Logger.getLogger(BuyAndHire.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(this, "Something wrong with Network.");
                System.exit(1);
            }
        } else if (e.getSource() == miner) {
            buttonPress();
            try {
                client.send("miner");
            } catch (IOException ex) {
                Logger.getLogger(BuyAndHire.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(this, "Something wrong with Network.");
                System.exit(1);
            }
        } else if (e.getSource() == attack) {
            buttonPress();
            worldPanel.hideGrids();
            try {
                client.send("attack");
            } catch (IOException ex) {
                Logger.getLogger(BuyAndHire.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(this, "Something wrong with Network.");
                System.exit(1);
            }
        } else if (e.getSource() == cancelBuild) {
            buttonRelease();
            try {
                client.send("cancel");
            } catch (IOException ex) {
                Logger.getLogger(BuyAndHire.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(this, "Something wrong with Network.");
                System.exit(1);
            }
        }
    }

    public void buttonPress() {
        worldPanel.showGrids();
        worldPanel.clearSelectedObj();
        laser.setEnabled(false);
        miner.setEnabled(false);
        attack.setEnabled(false);
        cancelBuild.setEnabled(true);
        worldPanel.setButtonClicked(true);
        bottomPanel.showGameInfo();
    }

    public void buttonRelease() {
        worldPanel.hideGrids();
        worldPanel.clearSelectedObj();
        laser.setEnabled(true);
        miner.setEnabled(true);
        attack.setEnabled(true);
        cancelBuild.setEnabled(false);
        worldPanel.setButtonClicked(false);
    }
}
